import 'package:dio/dio.dart';
import 'package:common/bloc/user_bloc.dart';

typedef TokenHeaderParamsClosure = Future<Map<String, dynamic>> Function(
    RequestOptions options);

class TokenHeaderInterceptor extends Interceptor {
  static String devToken;
  static String tokenKey = 'Authorization';
  static TokenHeaderParamsClosure headerParamsClosure;
  static bool shouldUseDevToken = false;
  @override
  Future onRequest(RequestOptions options) async {
    Map<String, dynamic> header = {};
    var token = UserBloc.token;
    if (shouldUseDevToken &&
        (token == null || token.isEmpty) &&
        (devToken != null && devToken.isNotEmpty)) {
      token = devToken;
    }
    if (token != null) {
      header['Authorization'] = token;
    }
    if (headerParamsClosure != null) {
      final param = await headerParamsClosure(options);
      header.addAll(param);
    }
    options.headers.addAll(header);
    return Future.value(options);
  }
}
